package com.boomsecret.thread;

/**
 * Created by wangjinliang on 2018/1/25.
 */
public class ThreadDemo {
    private volatile static boolean flag = true;
    private volatile static boolean flagB = true;
    private volatile static boolean flagC = true;

    public static void main(String[] args) throws Exception {
        Thread threadA = new Thread(new Runnable() {
            @Override
            public void run() {
                while (flag);
            }
        });
        Thread threadB = new Thread(new Runnable() {
            @Override
            public void run() {
                while (flagB);
                try {
                    threadA.join(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                while (flagC);
            }
        });
        threadA.start();
        Thread.sleep(1000);
        threadB.start();
        println(threadB.getState());
        flagB = false;
        Thread.sleep(1000);
        println(threadB.getState());
        flag = false;
        Thread.sleep(1000);
        println(threadB.getState());
        flagC = false;
    }

    /**
     * WAITING --> RUNNABLE
     */
//    public static void main(String[] args) throws Exception {
//        Thread threadA = new Thread(()->{
//            while (flag);
//        });
//        Thread threadB = new Thread(()->{
//            while (flagB);
//            try {
//                threadA.join();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            while (flagC);
//        });
//        threadA.start();
//        Thread.sleep(1000);
//        threadB.start();
//        println(threadB.getState());
//        flagB = false;
//        Thread.sleep(1000);
//        println(threadB.getState());
//        flag = false;
//        Thread.sleep(1000);
//        println(threadB.getState());
//        flagC = false;
//    }

    /**
     * RUNNABLE --> WAITING
     */
//    public static void main(String[] args) throws Exception {
//        Thread threadA = new Thread(()->{
//            while (flag);
//        });
//        Thread threadB = new Thread(()->{
//            while (flagB);
//            try {
//                threadA.join();
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//        });
//        threadA.start();
//        Thread.sleep(1000);
//        threadB.start();
//        println(threadB.getState());
//        flagB = false;
//        Thread.sleep(1000);
//        println(threadB.getState());
//        flag = false;
//    }

    /**
     * BLOCKED --> RUNNABLE
     */
//    public static void main(String[] args) throws Exception {
//        Thread threadA = new Thread(()->{
//            while (flag);
//            getRes();
//            while (flagB);
//        });
//        Thread threadB = new Thread(()->{
//            getRes();
//        });
//        threadB.start();
//        threadA.start();
//        Thread.sleep(1000);
//        flag = false;
//        Thread.sleep(1000);
//        println(threadA.getState());
//        Thread.sleep(5000);
//        println(threadA.getState());
//        flagB = false;
//    }

    /**
     * RUNNABLE --> BLOCKED
     */
//    public static void main(String[] args) throws Exception {
//        Thread threadA = new Thread(()->{
//            while (flag);
//            getRes();
//        });
//        Thread threadB = new Thread(()->{
//            getRes();
//        });
//        threadA.start();
//        println(threadA.getState());
//        threadB.start();
//        Thread.sleep(1000);
//        flag = false;
//        Thread.sleep(1000);
//        println(threadA.getState());
//    }

    /**
     * RUNNABLE --> TERMINATED
     */
//    private volatile static boolean flag = true;
//    public static void main(String[] args) throws Exception {
//        Thread thread = new Thread(()->{
//            while (flag) {}
//        });
//        thread.start();
//        println(thread.getState());
//        flag = false;
//        Thread.sleep(1000);
//        println(thread.getState());
//    }

    /**
     * NEW --> RUNNABLE
     */
//    private volatile static boolean flag = true;
//    public static void main(String[] args) {
//        Thread thread = new Thread(()->{
//            while (flag) {
//            }
//        });
//        println(thread.getState());
//        thread.start();
//        println(thread.getState());
//        flag = false;
//    }

    public static synchronized void getRes() {
        try {
            Thread.sleep(3000);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void println(Object msg) {
        System.out.println(msg);
    }
}
